﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using LuaInterface;

namespace PengLuaKit
{
    //
    // TickManager: update event and time event manager
    // @author harry
    //
    public class TickManager : MonoBehaviour
    {
        private static TickManager mInstance = null;
        public static TickManager Instance()
        {
            return mInstance;
        }

        [NoToLua]
        void Awake()
        {
            if (mInstance != null && mInstance != this)
            {
                Destroy(this);
                return;
            }

            mInstance = this;
            DontDestroyOnLoad(gameObject);
        }

        // Use this for initialization
        [NoToLua]
        void Start()
        {

        }

        #region when frame tick, update all functions in list

        private List<LuaFunction> mListOnUpdateTickHandler = new List<LuaFunction>();
        private List<LuaFunction> mListOnFixedUpdateTickHandler = new List<LuaFunction>();

        //
        // @api
        //
        public void AddFrameTickListener(LuaFunction luaFun, bool isFixedUpdate)
        {
            if (isFixedUpdate)
            {
                if (mListOnFixedUpdateTickHandler.IndexOf(luaFun) < 0)
                {
                    mListOnFixedUpdateTickHandler.Add(luaFun);
                }
            }
            else
            {
                if (mListOnUpdateTickHandler.IndexOf(luaFun) < 0)
                {
                    mListOnUpdateTickHandler.Add(luaFun);
                }
            }
        }

        //
        // @api
        //
        public void RemoveFrameTickListener(LuaFunction luaFun)
        {
            int index = -1;
            index = mListOnFixedUpdateTickHandler.IndexOf(luaFun);
            if (index >= 0)
            {
                mListOnFixedUpdateTickHandler.Remove(luaFun);
                luaFun.Dispose();// if remove luaFunction, will dispose it
                luaFun = null;
                return;
            }

            index = mListOnUpdateTickHandler.IndexOf(luaFun);
            if (index >= 0)
            {
                mListOnUpdateTickHandler.Remove(luaFun);
                luaFun.Dispose();
                luaFun = null;
            }
        }

        // Update is called once per frame
        [NoToLua]
        void Update()
        {
			for (int k=0; k< mListOnUpdateTickHandler.Count; k++)
            {
				LuaFunction item = mListOnUpdateTickHandler[k];
                if (item != null)
                    item.Call(Time.deltaTime);
            }
        }

        [NoToLua]
        void FixedUpdate()
        {
			for (int k=0; k< mListOnFixedUpdateTickHandler.Count; k++)
			{
				LuaFunction item = mListOnFixedUpdateTickHandler[k];
				if (item != null)
					item.Call(Time.deltaTime);
			}
        }

        #endregion

        [NoToLua]
        void OnDestroy()
        {
            if (mInstance = this)
            {
                // dispose this manager
                foreach (var item in mListOnFixedUpdateTickHandler)
                {
                    if (item != null) item.Dispose();
                }

                foreach (var item in mListOnUpdateTickHandler)
                {
                    if (item != null) item.Dispose();
                }

                mListOnFixedUpdateTickHandler.Clear();
                mListOnUpdateTickHandler.Clear();

            }
        }



    }

}